﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.CommandUIDefinitionClasses
{
    public class Spinner : ICommandUIDefinition, IControlsChild, IParentable, IXmlBuildable, IChildDeletable
    {
        public int? AccelerationInterval { get; set; }
        public string AltDownArrow { get; set; }
        public string AltUpArrow { get; set; }
        public string Command { get; set; }
        public string DefaultUnit { get; set; }
        public decimal? DefaultValue { get; set; }
        public string Id { get; set; }
        public string ImeEnabled { get; set; }
        public int? MultiplierInterval { get; set; }
        public string QueryCommand { get; set; }
        public int? Sequence { get; set; }
        public string TemplateAlias { get; set; }
        public string ToolTipDescription { get; set; }
        public string ToolTipHelpKeyWord { get; set; }
        public string ToolTipImage32by32 { get; set; }
        public string ToolTipImage32by32Class { get; set; }
        public int? ToolTipImage32by32Left { get; set; }
        public int? ToolTipImage32by32Top { get; set; }
        public string ToolTipShortcutKey { get; set; }
        public string ToolTipTitle { get; set; }
        public List<Unit> Units { get; set; }

        public Spinner()
        {
            this.Units = new List<Unit>();
        }

        public Spinner(XmlNode node)
        {
            try
            {
                this.AccelerationInterval = XmlHelper.GetNullableIntAttribute(node, "AccelerationInterval");
                this.AltDownArrow = XmlHelper.GetAttribute(node, "AltDownArrow");
                this.AltUpArrow = XmlHelper.GetAttribute(node, "AltUpArrow");
                this.Command = XmlHelper.GetAttribute(node, "Command");
                this.DefaultUnit = XmlHelper.GetAttribute(node, "DefaultUnit");
                this.DefaultValue = XmlHelper.GetNullableDecimalAttribute(node, "DefaultValue");
                this.Id = XmlHelper.GetAttribute(node, "Id");
                this.ImeEnabled = XmlHelper.GetAttribute(node, "ImeEnabled");
                this.MultiplierInterval = XmlHelper.GetNullableIntAttribute(node, "MultiplierInterval");
                this.QueryCommand = XmlHelper.GetAttribute(node, "QueryCommand");
                this.Sequence = XmlHelper.GetNullableIntAttribute(node, "Sequence");
                this.TemplateAlias = XmlHelper.GetAttribute(node, "TemplateAlias");
                this.ToolTipDescription = XmlHelper.GetAttribute(node, "ToolTipDescription");
                this.ToolTipHelpKeyWord = XmlHelper.GetAttribute(node, "ToolTipHelpKeyWord");
                this.ToolTipImage32by32 = XmlHelper.GetAttribute(node, "ToolTipImage32by32");
                this.ToolTipImage32by32Class = XmlHelper.GetAttribute(node, "ToolTipImage32by32Class");
                this.ToolTipImage32by32Left = XmlHelper.GetNullableIntAttribute(node, "ToolTipImage32by32Left");
                this.ToolTipImage32by32Top = XmlHelper.GetNullableIntAttribute(node, "ToolTipImage32by32Top");
                this.ToolTipShortcutKey = XmlHelper.GetAttribute(node, "ToolTipShortcutKey");
                this.ToolTipTitle = XmlHelper.GetAttribute(node, "ToolTipTitle");

                this.Units = new List<Unit>();
                foreach (XmlNode childNode in node.SelectNodes("Unit"))
                {
                    Unit unit = new Unit(childNode);
                    this.Units.Add(unit);
                }

                if (this.Units.Count == 0)
                {
                    Unit unit = new Unit();
                    this.Units.Add(unit);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode commandUINode)
        {
            try
            {
                XmlNode spinnerNode = XmlHelper.AddNode(commandUINode, "Spinner");
                XmlHelper.AddAttribute(spinnerNode, "AccelerationInterval", this.AccelerationInterval);
                XmlHelper.AddAttribute(spinnerNode, "AltDownArrow", this.AltDownArrow);
                XmlHelper.AddAttribute(spinnerNode, "AltUpArrow", this.AltUpArrow);
                XmlHelper.AddAttribute(spinnerNode, "Command", this.Command);
                XmlHelper.AddAttribute(spinnerNode, "DefaultUnit", this.DefaultUnit);
                XmlHelper.AddAttribute(spinnerNode, "DefaultValue", this.DefaultValue);
                XmlHelper.AddAttribute(spinnerNode, "Id", this.Id);
                XmlHelper.AddAttribute(spinnerNode, "ImeEnabled", this.ImeEnabled);
                XmlHelper.AddAttribute(spinnerNode, "MultiplierInterval", this.MultiplierInterval);
                XmlHelper.AddAttribute(spinnerNode, "QueryCommand", this.QueryCommand);
                XmlHelper.AddAttribute(spinnerNode, "Sequence", this.Sequence);
                XmlHelper.AddAttribute(spinnerNode, "TemplateAlias", this.TemplateAlias);
                XmlHelper.AddAttribute(spinnerNode, "ToolTipDescription", this.ToolTipDescription);
                XmlHelper.AddAttribute(spinnerNode, "ToolTipHelpKeyWord", this.ToolTipHelpKeyWord);
                XmlHelper.AddAttribute(spinnerNode, "ToolTipImage32by32", this.ToolTipImage32by32);
                XmlHelper.AddAttribute(spinnerNode, "ToolTipImage32by32Class", this.ToolTipImage32by32Class);
                XmlHelper.AddAttribute(spinnerNode, "ToolTipImage32by32Left", this.ToolTipImage32by32Left);
                XmlHelper.AddAttribute(spinnerNode, "ToolTipImage32by32Top", this.ToolTipImage32by32Top);
                XmlHelper.AddAttribute(spinnerNode, "ToolTipShortcutKey", this.ToolTipShortcutKey);
                XmlHelper.AddAttribute(spinnerNode, "ToolTipTitle", this.ToolTipTitle);

                foreach (Unit unit in this.Units)
                {
                    unit.AddNode(spinnerNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("Spinner");
                node.Tag = this;
                node.ContextMenuStrip = ContextMenuStripHelper.BuildUICommandDefContextMenuStrip(node, new string[] { "Unit" }, true);

                if (this.Units.Count == 1)
                {
                    node.ContextMenuStrip.Items[1].Enabled = false;
                }

                foreach (Unit unit in this.Units)
                {
                    unit.BuildTreeViewElement(node);
                }

                parentNode.Nodes.Add(node);

                node.TreeView.NodeMouseClick += new TreeNodeMouseClickEventHandler(ContextMenuStripHelper.TreeViewRightClick);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddChild(ICommandUIDefinition child)
        {
            try
            {
                if (!(child is Unit))
                {
                    throw new Exception("Unsupported child type: " + child.GetType().ToString());
                }
                else
                {
                    if (this.Units == null)
                    {
                        this.Units = new List<Unit>();
                    }

                    this.Units.Add((Unit)child);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is Unit)
                {
                    this.Units.Remove((Unit)o);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
